#
# Makefile
#
# Main makefile for the project.
#

#
# Includes.
#
include Common.mk
include Boost.mk
include CppUnit.mk

#
# Set the default goal.
#
.DEFAULT_GOAL = help


#
# Variables.
#

#
# Define some variables for targets.
#
DOC_TARGET  = $(DOC_DIR)/Informe.pdf
MAIN_TARGET = $(BIN_DIR)/tp1
TEST_TARGET = $(BIN_DIR)/tp1_test

#
# Define compiler options for include and library path.
#
INCLUDE_PATH = -I$(INC_DIR)
LIBRARY_PATH = -L$(LIB_DIR)

#
# Define C++ compiler flags.
#
CXXFLAGS = -O2 -ggdb -Wall -fmessage-length=0

#
# Define the libraries required by the project's main executable.
#
LIBRARIES = -ldl -lboost_program_options

#
# Define the libraries required by the project's test executable.
#
TEST_LIBRARIES = $(LIBRARIES) -lcppunit

#
# C++ header files.
#
HEADERS = $(addprefix $(CPP_SRC_DIR)/,City.h Daze.h Model.h Program.h Reader.h Real.h Trajectory.h Vector.h )

#
# LaTeX source files.
#
LATEX_SOURCES = $(addprefix $(SRC_DIR)/latex/,Informe.tex appendices.tex discussion.tex build.tex conclusions.tex development.tex enunciado.tex frontpage.tex overview.tex references.tex results.tex run.tex source.tex templates.tex tp1_2_2012_1.tex tp1_2_2012_2.tex)

#
# Project's main executable object file.
#
MAIN_OBJ = $(addprefix $(OBJ_DIR)/, tp1.o)

#
# Project's test executable object file.
#
TEST_OBJ = $(addprefix $(OBJ_DIR)/, tp1_test.o)

#
# Objects shared by project's main and test executables.
#
SHARED_OBJS = $(addprefix $(OBJ_DIR)/, City.o Daze.o Program.o Reader.o Real.o Trajectory.o Vector.o)


#
# Canned Recipes.
#

#
# Build PDF
#
define build_pdf
	@cd src/latex;						\
		pdflatex Informe.tex;				\
		cd ../../;					\
		mv src/latex/Informe.pdf doc/Informe.pdf
endef


#
# Targets.
#

#
# Define the list of .PHONY targets.
#
.PHONY: all check clean distclean doc-init main help pdf realclean test viewpdf

#
# all target.
#
# Builds both main and test executables.
#
all: main test

#
# check target.
#
# Runs project's tests.
#
check: test
	@$(TEST_TARGET)

#
# clean target.
#
# Deletes project generated files.
#
clean:
	@rm -rf $(BIN_DIR)
	@rm -rf $(OBJ_DIR)
	@rm -rf $(DOC_DIR)
	@rm -f $(SRC_DIR)/latex/*.aux
	@rm -f $(SRC_DIR)/latex/*.lo*
	@rm -f $(SRC_DIR)/latex/*.toc

#
# distclean target.
#
# Deletes project generated files and dependencies directories.
#
distclean: clean
	@rm -rf $(INC_DIR)
	@rm -rf $(LIB_DIR)
	@rm -rf $(BOOST_DIST_DIR)
	@rm -rf $(CPPUNIT_DIST_DIR)

#
# Init Documentation target.
#
# LaTeX needs several output files as input for building
# document elements such as the table of contents. Run
# the build_pdf several times in order to produce all
# the required intermediate files.
#
doc-init: | $(DOC_DIR)
	@$(build_pdf)
	@$(build_pdf)
	@$(build_pdf)

#
# help target.
#
# Displays a list of the available targets.
#
help:
	@echo
	@echo "Target\t\tDescription"
	@echo "------\t\t-----------"
	@echo "all\t\tBuilds both main and test project executables."
	@echo "check\t\tBuilds and runs project tests."
	@echo "clean\t\tDeletes project executables and object files."
	@echo "distclean\tSame as clean but also deletes $(INC_DIR), $(LIB_DIR), and the directories"
	@echo "\t\twhere the dependencies distributions have been extracted."
	@echo "doc-init\tGenerates several files needed by LaTeX to generate the report."
	@echo "help\t\tDisplays this message (Default)."
	@echo "main\t\tBuilds the main project executable."
	@echo "pdf\t\tBuilds the project report in PDF."
	@echo "realclean\tDeletes everything but the project code."
	@echo "test\t\tBuilds the project test executable."
	@echo "viewpdf\t\tOpens the generated project report."
	@echo

#
# main target.
#
# Alias for the $(MAIN_TARGET) target.
#
main: $(MAIN_TARGET)

#
# pdf target.
#
# Alias for the $(DOC_TARGET) target.
#
pdf: $(DOC_TARGET)

#
# realclean target.
#
# Deletes everything but the project sources.
#
realclean: distclean
	@rm -rf $(DIST_DIR)


#
# test target.
#
# Alias for the $(TEST_TARGET) target.
#
test: $(TEST_TARGET)

#
# viewpdf target.
#
# Opens the project report PDF file in the default viewer.
#
viewpdf: pdf
	@$(call open) $(DOC_DIR)/Informe.pdf

#
# $(DOC_TARGET) target.
#
# Generates the project report PDF document.
#
$(DOC_TARGET): $(LATEX_SOURCES) | $(DOC_DIR)
	@echo Compiling LaTeX sources...
	@$(build_pdf)

#
# $(MAIN_TARGET)
#
# Builds the project main executable.
#
$(MAIN_TARGET): $(SHARED_OBJS) $(MAIN_OBJ) | $(BOOST) $(BIN_DIR)
	$(CXX) -o $@ $^ $(CXXFLAGS) $(LIBRARY_PATH) $(LIBRARIES)

#
# $(TEST_TARGET)
#
# Builds the project test executable.
#
$(TEST_TARGET): $(SHARED_OBJS) $(TEST_OBJ) | $(BOOST) $(CPPUNIT)  $(BIN_DIR)
	$(CXX) -o $@ $^ $(CXXFLAGS) $(LIBRARY_PATH) $(TEST_LIBRARIES)

#
# Rule for building the object files.
#
$(OBJ_DIR)/%.o: $(CPP_SRC_DIR)/%.cpp | $(BOOST) $(CPPUNIT) $(OBJ_DIR)
	$(CXX) -c -o $@ $^ $(CXXFLAGS) $(INCLUDE_PATH)

